﻿using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Text;
using Dcms.Common;
using Dcms.BLL;
using Dcms.Model;

public partial class Admin_MainFrom : System.Web.UI.Page
{
    private static readonly string WEB_PATH = ConfigurationManager.AppSettings["WebPath"];
    private static readonly string WEB_Language = ConfigurationManager.AppSettings["Language"];
    private static readonly string WEB_LanguageNmae = ConfigurationManager.AppSettings["LanguageNmae"];
    private static readonly string WEB_DefaultLanguage = ConfigurationManager.AppSettings["DefaultLanguage"];
    Dcms_PermissionBLL perBll = new Dcms_PermissionBLL();

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            CompetenceLib CL = new CompetenceLib();
            CL.IsLoginMain();
            if (!IsPostBack)
            {
                if (Request.QueryString["Language"] == null)
                {
                    if (Session["DCMSLANGUAGENAME"] == null)
                    {
                        String[] DelanAr = WEB_DefaultLanguage.Split(',');
                        Session.Add("DCMSLANGUAGE", DelanAr[0]);
                        Session.Add("DCMSLANGUAGENAME", DelanAr[1]);
                    }
                }
                else {
                    Session.Add("DCMSLANGUAGE", Request.QueryString["Language"].ToString());
                    Session.Add("DCMSLANGUAGENAME", Utils.UrlDecode(Request.QueryString["LangName"].ToString()));
                    Response.Redirect("MainFrom.aspx",false);
                }
                labLang.Text = Session["DCMSLANGUAGENAME"].ToString();

                String TempLang = "\t\t\t<div onMouseOver=\"this.className='over'\" onMouseOut=\"this.className=''\" onClick=\"location.href='{$HREF}'\">\n";
                TempLang += "\t\t\t\t<img src=\"images/icon-{$LANAGE}.gif\" align=\"absmiddle\" /> {$LANAGENAME}\n\t\t\t\t</div>\n";

                String[] lanAr = WEB_Language.Split(',');
                String[] lanNAr = WEB_LanguageNmae.Split(',');

                if (lanAr.Length > 0) {
                    String Lang = "";
                    for (int i = 0; i < lanAr.Length; i++) {
                        Lang += TempLang;
                        Lang = Lang.Replace("{$LANAGE}", lanAr[i]);
                        Lang = Lang.Replace("{$LANAGENAME}", lanNAr[i]);
                        Lang = Lang.Replace("{$HREF}", "?Language=" + lanAr[i] + "&LangName=" + Utils.UrlEncode(lanNAr[i]));
                    }

                    String TempStr = "<div class=\"list\" onMouseOver=\"MM_showHideLayers('Top_navig','','show')\" onMouseOut=\"MM_showHideLayers('Top_navig','','hide')\">\n";
                    TempStr += "\t<div id=\"Top_navig\" class=\"lan\">\n";
                    TempStr += Lang;
                    TempStr += "\t</div>";
                    TempStr += "\t<a href=\"#\">语言选择<img src=\"images/icon-other.gif\" align=\"absmiddle\" /></a>\n";
                    TempStr += "</div>\n";

                    Lit_Lang.Text = TempStr;
                }

                this.litName.Text = CL.getSessionModle().LoginName;

                //加载菜单
                string Language = "";
                if (Session["DCMSLANGUAGE"] != null)
                {
                    Language = Session["DCMSLANGUAGE"].ToString();
                }

                string strDesignRoleID = ConfigurationManager.AppSettings["DesignRoleID"];
                if (CL.getSessionModle().IsAdministrator || CL.getSessionModle().RoleID == Convert.ToInt32(strDesignRoleID))
                {
                    //IList<Dcms_PermissionModel> FModelAl = perBll.GetUserPermissionModel(0);
                    //StringBuilder sb = new StringBuilder();
                    //for (int i = 0; i < FModelAl.Count; i++)
                    //{
                    //    Dcms_PermissionModel fmm = FModelAl[i];
                    //    sb.Append("<div id=\"Menu\">" + "\n");
                    //    sb.Append("\t" + "<div id=\"MenuRoot\" class=\"menuRootBg\" onclick=\"MM_OpenCLoseLayers(" + i + ",'MenuSub','leftMenu')\">" + fmm.Permission_Name + "</div>");
                    //    sb.Append("\t" + "<div id=\"MenuSub\" class=\"menusubBtn\">" + "\n");
                    //    sb.Append("\t\t" + "<div id=\"MenuLine\"><img src=\"images/menu_topline.gif\" width=\"182\" height=\"5\" alt=\"\" /></div>" + "\n");
                    //    IList<Dcms_PermissionModel> CModelAl = perBll.GetUserPermissionModel(fmm.Permission_Id);
                    //    for (int j = 0; j < CModelAl.Count; j++)
                    //    {
                    //        Dcms_PermissionModel cmm = CModelAl[j];
                    //        sb.Append("\t\t" + "<a href=\"#\" onclick=\"MM_OpenTabLayers('" + cmm.Permission_Name + "','" + cmm.Permission_Url + "?id=" + cmm.Permission_Id.ToString() + getTopCateID(cmm.Permission_Flag, cmm.Permission_ModelKeyId, Language) + "','" + cmm.Permission_Flag + cmm.Permission_Id + "')\">" + cmm.Permission_Name + "</a>" + "\n");
                    //    }
                    //    sb.Append("\t" + "</div>");
                    //    sb.Append("</div>");
                    //}
                    //this.litMenu.Text = sb.ToString();

                    Dcms.BLL.Dcms_ModelBLL ModelBll = new Dcms_ModelBLL();
                 
                    StringBuilder sb = new StringBuilder();
                    //sb.Append("<div id=\"Menu\">" + "\n");
                    //sb.Append("\t" + "<div id=\"MenuRoot\" class=\"menuRootBg\" onclick=\"MM_OpenCLoseLayers(0,'MenuSub','leftMenu')\">系统配置</div>");
                    //sb.Append("\t" + "<div id=\"MenuSub\" class=\"menusubBtn\">" + "\n");
                    //sb.Append("\t\t" + "<div id=\"MenuLine\"><img src=\"images/menu_topline.gif\" width=\"182\" height=\"5\" alt=\"\" /></div>" + "\n");

                    //DataSet ds = ModelBll.GetList("Model_Type in ('CateSYSTEM','WEBSITEINFO')");
                    //if (ds != null && ds.Tables[0]!=null)
                    //{
                    //    foreach (DataRow dr in ds.Tables[0].Rows)
                    //    {
                    //        sb.Append("\t\t" + "<a href=\"#\" onclick=\"MM_OpenTabLayers('" + dr["Model_Name"].ToString() + "','" + dr["Model_Url"].ToString() + "?id=" + dr["Model_ID"].ToString() + "','" + dr["Model_Type"].ToString() + dr["Model_ID"].ToString() + "')\">" + dr["Model_Name"].ToString() + "</a>" + "\n");
                    //    }
                    //}

                    //sb.Append("\t" + "</div>");
                    //sb.Append("</div>");
                    sb.Append("<div id=\"Menu\">" + "\n");
                    sb.Append("\t" + "<div id=\"MenuRoot\" class=\"menuRootBg\" onclick=\"MM_OpenCLoseLayers(1,'MenuSub','leftMenu')\">系统管理</div>");
                    sb.Append("\t" + "<div id=\"MenuSub\" class=\"menusubBtn\">" + "\n");
                    sb.Append("\t\t" + "<div id=\"MenuLine\"><img src=\"images/menu_topline.gif\" width=\"182\" height=\"5\" alt=\"\" /></div>" + "\n");


                    DataSet ds = new DataSet();
                    if (CL.getSessionModle().RoleID == Convert.ToInt32(strDesignRoleID))
                    {
                        ds = ModelBll.GetList("Model_Type in ('SYSTEM','CateSYSTEM','WEBSITEINFO') and Model_ID <> 145");
                    }
                    else
                    {
                        ds = ModelBll.GetList("Model_ID in (2, 3, 14, 145) ");
                    }
                    if (ds != null && ds.Tables[0] != null)
                    {
                        foreach (DataRow dr in ds.Tables[0].Rows)
                        {
                            sb.Append("\t\t" + "<a href=\"#\" onclick=\"MM_OpenTabLayers('" + dr["Model_Name"].ToString() + "','" + dr["Model_Url"].ToString() + "?id=" + dr["Model_ID"].ToString() + "','" + dr["Model_Type"].ToString() + dr["Model_ID"].ToString() + "')\">" + dr["Model_Name"].ToString() + "</a>" + "\n");
                        }
                    }
                    sb.Append("\t\t" + "<a href=\"#\" onclick=\"MM_OpenTabLayers('cateview','Modle/CateGory/CateView.aspx','cateview')\">树视图</a>" + "\n");
                    sb.Append("\t" + "</div>");
                    sb.Append("</div>");

                   this.litMenu.Text = sb.ToString();

                }
                else
                {
                    //Dcms_PerRoleBLL pBLL = new Dcms_PerRoleBLL();
                    //string pIdstr = string.Empty;
                    //pIdstr = pBLL.GetPIdByRId(CL.getSessionModle().RoleID, "PerRole_IsRead");

                    //IList<Dcms_PermissionModel> FModelAl = perBll.GetUserPermissionModelByRoleId(0, pIdstr);         
                    //StringBuilder sb = new StringBuilder();
                    //for (int i = 0; i < FModelAl.Count; i++)
                    //{
                    //    Dcms_PermissionModel fmm = FModelAl[i];
                    //    sb.Append("<div id=\"Menu\">" + "\n");
                    //    sb.Append("\t" + "<div id=\"MenuRoot\" class=\"menuRootBg\" onclick=\"MM_OpenCLoseLayers(" + i + ",'MenuSub','leftMenu')\">" + fmm.Permission_Name + "</div>");
                    //    sb.Append("\t" + "<div id=\"MenuSub\" class=\"menusubBtn\">" + "\n");
                    //    sb.Append("\t\t" + "<div id=\"MenuLine\"><img src=\"images/menu_topline.gif\" width=\"182\" height=\"5\" alt=\"\" /></div>" + "\n");
                    //    IList<Dcms_PermissionModel> CModelAl = perBll.GetUserPermissionModelByRoleId(fmm.Permission_Id, pIdstr);
                    //    for (int j = 0; j < CModelAl.Count; j++)
                    //    {
                    //        Dcms_PermissionModel cmm = CModelAl[j];
                    //        sb.Append("\t\t" + "<a href=\"#\" onclick=\"MM_OpenTabLayers('" + cmm.Permission_Name + "','" + cmm.Permission_Url + "?id=" + cmm.Permission_Id.ToString() + getTopCateID(cmm.Permission_Flag, cmm.Permission_ModelKeyId, Language) + "','" + cmm.Permission_Flag + cmm.Permission_Id + "')\">" + cmm.Permission_Name + "</a>" + "\n");
                    //    }
                    //    sb.Append("\t" + "</div>");
                    //    sb.Append("</div>");              
                    //}
                    //this.litMenu.Text = sb.ToString();
                    ///如果不是管理权限，跳回分类管理
                    Response.Redirect("MainFrom.aspx",true);                                            
                }
            }
        }
        catch (Exception ex)
        {
            Log.LogHandler.LogError("Admin_MainFrom.Page_Load", ex);
            throw ex;
        }
    }

    private string getTopCateID(string ModelFlag, int ModelKeyID, string strLanguage)
    {
        Dcms_CateBLL cBll = new Dcms_CateBLL();
        Dcms_CateModel cModel = cBll.GetModelByFlag(ModelFlag, ModelKeyID, strLanguage);
        if (cModel != null)
        {
            return "&TopCateID=" + cModel.Cate_ID;
        }
        else
        {
            return "";
        }
    }
}
